package com.xjc.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xjc.business.entity.domain.AllStudentRegsiterRelations;
import com.xjc.business.entity.domain.CollageAdminRelations;
import com.xjc.business.entity.domain.SchoolAdminRelations;
import com.xjc.business.entity.domain.SchoolNews;
import com.xjc.business.entity.po.NewsPo;
import com.xjc.business.entity.vo.PageVo;
import com.xjc.business.entity.vo.NewsVo;
import com.xjc.business.service.*;
import com.xjc.business.mapper.SchoolNewsMapper;
import com.xjc.system.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author root
* @description 针对表【school_news(新闻表)】的数据库操作Service实现
* @createDate 2025-01-25 10:24:32
*/
@Service
public class SchoolNewsServiceImpl extends ServiceImpl<SchoolNewsMapper, SchoolNews>
    implements SchoolNewsService {

    @Autowired
    @Lazy
    WxUserService wxUserService;

    @Autowired
    @Lazy
    AllStudentRegsiterRelationsService allStudentRegsiterRelationsService;

    @Autowired
    @Lazy
    SchoolAdminRelationsService schoolAdminRelationsService;

    @Autowired
    @Lazy
    CollageAdminRelationsService collageAdminRelationsService;

    /**
     * 分页查询
     * @param pageSize      每页大小
     * @param currentPage   当前页码
     * @param title         标题
     * @param schoolAdminId 学院管理员id
     * @return 数据
     */
    @Override
    public Result<PageVo<NewsVo>> getNewsPage(Integer pageSize, Integer currentPage, String title, Integer schoolAdminId) {
        LambdaQueryWrapper<SchoolNews> schoolNewsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        schoolNewsLambdaQueryWrapper.like(!title.isEmpty(),SchoolNews::getTitle,title);
        schoolNewsLambdaQueryWrapper.eq(SchoolNews::getPulishId,schoolAdminId);

        Page<SchoolNews> schoolNewsPage = new Page<>(currentPage, pageSize);
        page(schoolNewsPage,schoolNewsLambdaQueryWrapper);
        List<SchoolNews> schoolNewsPageRecords = schoolNewsPage.getRecords();
        List<NewsVo> newsVoList = schoolNewsPageRecords.stream().map(schoolNews -> {
            NewsVo newsVo = new NewsVo();
            newsVo.setId(schoolNews.getId());
            newsVo.setTitle(schoolNews.getTitle());
            newsVo.setMaskPic(schoolNews.getMaskPic());
            newsVo.setReadNum(schoolNews.getReadNum());
            newsVo.setCreateTime(schoolNews.getCreateTime());
            return newsVo;
        }).collect(Collectors.toList());

        PageVo<NewsVo> pageVo = new PageVo<>();
        pageVo.setCurrent(currentPage);
        pageVo.setSize(pageSize);
        pageVo.setTotal(schoolNewsPage.getTotal());
        pageVo.setPageCount(schoolNewsPage.getPages());
        pageVo.setData(newsVoList);

        return Result.okWithData(pageVo);
    }

    /**
     * 添加新闻
     *
     * @param newsPo 新闻内容
     * @return 结果
     */
    @Override
    public Result<String> addNews(NewsPo newsPo) {
        SchoolNews schoolNews = new SchoolNews();
        schoolNews.setTitle(newsPo.getTitle());
        schoolNews.setMaskPic(newsPo.getMaskPic());
        schoolNews.setContent(newsPo.getContent());
        schoolNews.setPulishId(newsPo.getPulishId());
        schoolNews.setType(newsPo.getType());
        schoolNews.setReadNum(0);
        schoolNews.setCreateTime(new Date());
        schoolNews.setUpdateTime(new Date());
        save(schoolNews);
        return Result.ok("新增成功");
    }

    /**
     * 阅读新闻
     *
     * @param id 新闻id
     * @return 结果
     */
    @Override
    public Result<String> readNews(Integer id) {
        synchronized (this){
            SchoolNews schoolNews = getById(id);
            if (schoolNews!=null){
                schoolNews.setReadNum(schoolNews.getReadNum()+1);
                updateById(schoolNews);
                return Result.okWithData("阅读成功");
            }
        }
        return null;
    }

    /**
     * 首页新闻列表
     *
     * @param id 微信id
     * @return 新闻列表
     */
    @Override
    public Result<List<NewsVo>> getNewsList(Integer id) {
        Integer stuId = wxUserService.getById(id).getStuId();

        LambdaQueryWrapper<AllStudentRegsiterRelations> allStudentRegsiterRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        allStudentRegsiterRelationsLambdaQueryWrapper.eq(AllStudentRegsiterRelations::getStuId,stuId);

        AllStudentRegsiterRelations studentFromDB = allStudentRegsiterRelationsService.getOne(allStudentRegsiterRelationsLambdaQueryWrapper);
        Integer schId = studentFromDB.getSchId();
        Integer colId = studentFromDB.getColId();

        List<Integer> collectIdList = new ArrayList<>();

        LambdaQueryWrapper<SchoolAdminRelations> schoolAdminRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        schoolAdminRelationsLambdaQueryWrapper.eq(SchoolAdminRelations::getSchId,schId);
        Integer schAdminId = schoolAdminRelationsService.getOne(schoolAdminRelationsLambdaQueryWrapper).getSchAdminId();
        collectIdList.add(schAdminId);

        LambdaQueryWrapper<CollageAdminRelations> collageAdminRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        collageAdminRelationsLambdaQueryWrapper.eq(CollageAdminRelations::getColId,colId);
        Integer colAdminId = collageAdminRelationsService.getOne(collageAdminRelationsLambdaQueryWrapper).getColAdminId();
        collectIdList.add(colAdminId);

        LambdaQueryWrapper<SchoolNews> schoolNewsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        schoolNewsLambdaQueryWrapper.in(SchoolNews::getPulishId,collectIdList);

        Page<SchoolNews> schoolNewsPage = new Page<>(1, 3);
        page(schoolNewsPage,schoolNewsLambdaQueryWrapper);

        List<SchoolNews> schoolNewsPageRecords = schoolNewsPage.getRecords();
        List<NewsVo> newsVoList = schoolNewsPageRecords.stream().map(schoolNews -> {
            NewsVo newsVo = new NewsVo();
            newsVo.setId(schoolNews.getId());
            newsVo.setTitle(schoolNews.getTitle());
            newsVo.setMaskPic(schoolNews.getMaskPic());
            newsVo.setReadNum(schoolNews.getReadNum());
            newsVo.setCreateTime(schoolNews.getCreateTime());
            return newsVo;
        }).collect(Collectors.toList());

        return Result.okWithData(newsVoList);
    }
}




